package edu.uts.reekie.designer.edit;

import org.eclipse.draw2d.ChopboxAnchor;
import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.AccessibleEditPart;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;

import edu.uts.reekie.designer.conceptual.edit.ViewComponentEditPolicy;
import edu.uts.reekie.designer.conceptual.figures.ViewComponentFigure;
import edu.uts.reekie.designer.conceptual.model.ViewComponent;
import edu.uts.reekie.designer.figures.FigureFactory;
import edu.uts.reekie.designer.figures.NameableFigure;
import edu.uts.reekie.designer.figures.NoteFigure;
import edu.uts.reekie.designer.model.ModelFactory;
import edu.uts.reekie.designer.model.Note;

public class NoteEditPart extends ViewNodeEditPart {
	
	public NoteEditPart(ModelFactory mf, FigureFactory ff){
		super(mf,ff);
	}

	protected void createEditPolicies(){
		super.createEditPolicies();
		
		installEditPolicy(EditPolicy.COMPONENT_ROLE, new ViewElementEditPolicy());
		installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, new NamedDirectEditPolicy());
		
	}
	
	public void performRequest(Request request){
		if (request.getType() == RequestConstants.REQ_DIRECT_EDIT)
			performDirectEdit();
	}
	
	private void performDirectEdit(){
				
		new NameEditManager(this,
				new NameCellEditorLocator(getFigureAs())).show();
	}
	
	
	private void updateName() {
		
		NameableFigure f = (NameableFigure)getFigureAs();
		Note n = getModelAs();
		f.setName(n.getName());
		
		//((ViewComponentFigure)getFigure()).setName(getModelAs<Note>().getName());
	}
	
public void propertyChange(java.beans.PropertyChangeEvent change){
		
		boolean doRefresh = false;
		
		if (change.getPropertyName().equals(Note.PROPERTY_NAME)) {
			updateName();
			doRefresh = true;
		}
		/*
		else if (change.getPropertyName().equals(Note.PROPERTY_CONNECTORS_SOURCE)) {
			refreshSourceConnections();
		} else if (change.getPropertyName().equals(Note.PROPERTY_CONNECTORS_TARGET)) {
			refreshTargetConnections();
		}
		*/
		
		if (doRefresh)
			refreshVisuals();
		else 
			super.propertyChange(change);
	}
	
	
	public Note getNoteModel() {
		return getModelAs();
	}
	
	@Override
	protected AccessibleEditPart createAccessible() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected IFigure createFigure() {
		NoteFigure nf = new NoteFigure();
		
		nf.setText(getNoteModel().getText());
		return nf;
	}
	
	
	protected ConnectionAnchor getConnectionAnchor() {
		
		NoteFigure f = getFigureAs();
		return new ChopboxAnchor(f.getAnchorFigure());
	}
	
	/**
	 * Returns the connection anchor for the given
	 * ConnectorEditPart's source. 
	 *
	 * @return  ConnectionAnchor.
	 */
	public ConnectionAnchor getSourceConnectionAnchor(ConnectionEditPart connEditPart) {
		return getConnectionAnchor();
	}

	/**
	 * Returns the connection anchor of a source connection which
	 * is at the given point.
	 * 
	 * @return  ConnectionAnchor.
	 */
	public ConnectionAnchor getSourceConnectionAnchor(Request request) {
		return getConnectionAnchor();
	}

	/**
	 * Returns the connection anchor for the given 
	 * ConnectorEditPart's target.
	 *
	 * @return  ConnectionAnchor.
	 */
	public ConnectionAnchor getTargetConnectionAnchor(ConnectionEditPart connEditPart) {
		return getConnectionAnchor();
	}

	/**
	 * Returns the connection anchor of a target connection which
	 * is at the given point.
	 *
	 * @return  ConnectionAnchor.
	 */
	public ConnectionAnchor getTargetConnectionAnchor(Request request) {
		return getConnectionAnchor();
	}
	

}
